package com.musichord.reader;

import javax.sound.midi.MidiMessage;

public class MidiMessageParser {

	private MidiMessageParser() {
	}

	public static boolean isMeta(MidiMessage me) {
		return Integer.toHexString(me.getStatus()).toLowerCase()
				.equals(MidiDefinition.METADATA);
	}

	public static String display(MidiMessage me) {
		StringBuilder sb = new StringBuilder();
		if (isMeta(me)) {
			sb.append(
					MidiDefinition.symbolMap.get(Integer.toHexString(
							me.getStatus()).toLowerCase())).append(":");
			String meta_type = Integer.toHexString(Byte.valueOf(
					me.getMessage()[1]).intValue());
			sb.append(MidiDefinition.symbolMap.get(meta_type.toLowerCase()))
					.append("=");
			// Skip the bytes for length
			int offset = 3;
			for (int i = 2; Byte.valueOf(me.getMessage()[i]).intValue() >= 128; i++) {
				offset++;
			}
			sb.append(new String(me.getMessage(), offset,
					me.getMessage().length - offset));
		} else {

		}
		return sb.toString();
	}
}
